#!/usr/bin/ruby

########################################
#        MFmixins.rb                                                                           
#        description;                                                                          
#            provide mixin functions for MFd.rb                                  
#        version           1.1
#        last modified     07/25/05                                                     
########################################

MIX_VERSION=1.1

#Do we want the ipv4 address space setup?
$ipv4int=nil

def mixins_ver
   return "\tMIXINS library version: #{MIX_VERSION}\n"
end

def get_tools
    # find location of all tools, add them to a global variable, deactivate any modules that the tools don't exist for
    # required tools
    # iptables, iptables-save, iptables-restore
    # cp, ifconfig, cat, echo, arp, halt, route, chmod, killall, snort, tc, grep, 
    # awk, ls, mii-tool, openssl, clear, zip, mv, touch, mkdir, mount, umount, ip vconfig

    if test(?x,"/sbin/ip6tables")
        $ip6tables="/sbin/ip6tables"
    elsif test(?x,"/usr/local/sbin/ip6tables")
        $ip6tables="/usr/local/sbin/ip6tables"
    else 
        $ip6tables=nil
    end

    if test(?x,"/sbin/iptables")
        $iptables="/sbin/iptables"
    elsif test(?x,"/usr/local/sbin/iptables")
        $iptables="/usr/local/sbin/iptables"
    else 
        $iptables=nil
    end
    
    if test(?x,"/sbin/iptables-save")
        $iptables_save="/sbin/iptables-save"
    elsif test(?x,"/usr/local/sbin/iptables-save")
        $iptables_save="/usr/local/sbin/iptables-save"
    else 
        $iptables_save=nil
    end
    
    if test(?x,"/sbin/iptables-restore")
        $iptables_restore="/sbin/iptables-restore"
    elsif test(?x,"/usr/local/sbin/iptables-restore")
        $iptables_restore="/usr/local/sbin/iptables-restore"
    else 
        $iptables_restore=nil
    end

    if test(?x,"/usr/sbin/dhcpd3")
        $dhcpd="/usr/sbin/dhcpd3"
    elsif test(?x,"/usr/sbin/dhcpd")
        $dhcpd="/usr/sbin/dhcpd"
    elsif test(?x,"/sbin/dhcpd")
        $dhcpd="/sbin/dhcpd"
    elsif test(?x,"/usr/local/sbin/dhcpd")
        $dhcpd="/usr/local/sbin/dhcpd"
    else 
        $dhcpd=nil
    end

    if test(?x,"/bin/cp")
        $cp="/bin/cp"
    else 
        $cp=nil
    end
    
    if test(?x,"/sbin/ifconfig")
        $ifconfig="/sbin/ifconfig"
    else 
        $ifconfig=nil
    end   
    
    if test(?x,"/bin/cat")
        $cat="/bin/cat"
    else 
        $cat=nil
    end   
    
    if test(?x,"/bin/echo")
        $echo="/bin/echo"
    else 
        $echo=nil
    end   
    
    if test(?x,"/sbin/arp")
        $arp="/sbin/arp"
    else 
        $arp=nil
    end   

    if test(?x,"/sbin/halt")
        $halt="/sbin/halt"
    else 
        $halt=nil
    end  

    if test(?x,"/sbin/route")
        $route="/sbin/route"
    else 
        $route=nil
    end  

    if test(?x,"/sbin/ip")
        $iproute="/sbin/ip -6 route"
    else 
        $route=nil
    end  

    if test(?x,"/bin/chmod")
        $chmod="/bin/chmod"
    else 
        $chmod=nil
    end  

    if test(?x,"/usr/bin/killall")
        $killall="/usr/bin/killall"
    else 
        $killall=nil
    end  

    if test(?x,"/bin/kill")
        $kill="/bin/kill"
    elsif test(?x, "/usr/bin/kill")
        $kill="/usr/bin/kill"
    else
        $kill=nil
    end  
    if test(?x,"/usr/bin/snort")
        $snort="/usr/bin/snort"
    elsif test(?x,"/usr/local/bin/snort")
        $snort="/usr/local/bin/snort"
    else 
        $snort=nil
    end  

    if test(?x,"/sbin/tc")
        $tc="/sbin/tc"
    else 
        $tc=nil
    end  

    if test(?x,"/bin/grep")
        $grep="/bin/grep"
    else 
        $grep=nil
    end  

    if test(?x,"/bin/ls")
        $ls="/bin/ls"
    else 
        $ls=nil
    end  

    if test(?x,"/bin/ps")
        $ps="/bin/ps"
    else 
        $ps=nil
    end  

    if test(?x,"/bin/awk")
        $awk="/bin/awk"
    else 
        $awk=nil
    end  

    if test(?x,"/sbin/mii-tool")
        $mii_tool="/sbin/mii-tool"
    else 
        $mii_tool=nil
    end  

    if test(?x,"/usr/bin/openssl")
        $openssl="/usr/bin/openssl"
    else 
        $openssl=nil
    end  

    if test(?x,"/usr/bin/clear")
        $clear="/usr/bin/clear"
    else 
        $clear=nil
    end  

    if test(?x,"/usr/bin/zip")
        $zip="/usr/bin/zip"
    else 
        $zip=nil
    end  

    if test(?x,"/bin/mv")
        $mv="/bin/mv"
    else 
        $mv=nil
    end  

    if test(?x,"/bin/tar")
        $tar="/bin/tar"
    else 
        $tar=nil
    end  

    if test(?x,"/bin/touch")
        $touch="/bin/touch"
    else 
        $touch=nil
    end  

    if test(?x,"/bin/mkdir")
        $mkdir="/bin/mkdir"
    else 
        $mkdir=nil
    end  

    if test(?x,"/bin/mount")
        $mount="/bin/mount"
    else 
        $mount=nil
    end  

    if test(?x,"/bin/umount")
        $umount="/bin/umount"
    else 
        $umount=nil
    end  

    if test(?x,"/sbin/ip")
        $ip="/sbin/ip"
    else 
        $ip=nil
    end  

    if test(?x,"/sbin/vconfig")
        $vconfig="/sbin/vconfig"
    else 
        $vconfig=nil
    end  
end

def active?(target)
    # check to see if the target is active
    if test(?r,"#{target}/active")
        f=File.open("#{target}/active").readline
        if f
            if f.downcase.include?("yes")
                return("TRUE")
            end
        end
    end
    return(nil)
end


def MFreadlines(file)
		if /\/\.svn$/ =~ file
			#print "#{file} includes svn\n"
			else
			#No SVN found in the name
	    if test(?r,file)
        f=Array.new
        f=File.open(file).readlines
        retary=Array.new
        f.each {|line|
            next if !line.index(/[a-zA-Z0-9]/)
            next if line.index(/^[#;|><!]/)
            next if line.index(/^[ ]+[#;|><!]/)
            next if line.index(/^[ \t]+[#;|><!]/)
            breaks=["#",";","|",">","<"]
            breaks.each {|bad|                       
                if line.include?(bad)
                    tmp=line.split(bad)
                    line=tmp[0]
                end
            }
            retary << line.chomp
        }
        return retary
			end
		end
    return(nil)
end

# the following lines snipped from Dan Debertin <airboss@nodewarrior.org>'s depreciated netmask-0.8, which is the only thing in the world that explained exactly what I was trying to do

def quad2num(ip)
    ip.split(/\./).map { |o| o.to_i }.pack("C4").unpack("N")[0]
end

def num2quad(ip)
    d = []
    4.times { |i| d.unshift((ip >> i * 8) & 0xff) }
    d.join('.')
end

def num2bin(ip)
    d = []
    4.times { |i| d.unshift((ip >> i * 8) & 0xff) }
    d.pack("C4")
end

def bin2num(ip)
    ip.unpack("N")[0]
end

def bits2num(m)
    (2 ** 32) - (2 ** (32 - m))
end

def num2bits(m)
    32 - (Math.log((2 ** 32) - m) / Math.log(2)).to_i
end

def network(addr, mask)
    addr & mask
end

def bits(m)
    num2bits(m)
end

def size(m)
    2 ** (32 - bits(m))
end

def broadcast(addr, mask)
    network(addr, mask) + (size(mask) - 1)
end

# end of snippage

def find_netspec(ip,netmask)
    mip = quad2num(ip)
    if netmask.index("\.")
        mnm=quad2num(netmask)
    else
        mnm=bits2num(netmask.gsub("/",""))
    end
    return ["#{num2quad(network(mip,mnm))}","#{num2quad(broadcast(mip,mnm))}"]
end

def find_network(ip,netmask)
    mip = quad2num(ip)
    if netmask.index("\.")
        mnm=quad2num(netmask)
    else
        mnm=bits2num(netmask.gsub("/",""))
    end
    return num2quad(network(mip,mnm))
end

def find_broadcast(ip,netmask)
    mip = quad2num(ip)
    if netmask.index("\.")
        mnm=quad2num(netmask)
    else
        mnm=bits2num(netmask.gsub("/",""))
    end
    return num2quad(broadcast(mip,mnm))
end

def ipv4(ip)
	if ip.include?"." or ip == "TRANS" or ip.include?"icmp "
		return true
	else
		return false
	end
end

def ipv6(ip)
	if ip.include?":" or ip.include?"icmpv6"
		return true
	else
		return false
	end
end
